package fina.indextree;


// 下标 +/- 最右侧的1 add+  sum-
public class Index2D {
    int N;
    int M;
    int[][] tree;

    public Index2D(int N,int M) {
        this.N = N + 1;
        this.M = M + 1;
        tree = new int[this.N][this.M];
    }
    public void add(int row,int col,int val){
        for(int i = row; i < N; i += (i & -i)){
            for(int j = col; j < M; j += (j & -j)){
                tree[i][j] += val;
            }
        }
    }
    public int sum(int row,int col){
        int res = 0;
        for(int i = row; i > 0; i -= (i & -i)){
            for(int j = col; j > 0; j -= (j & -j)){
                res += tree[i][j];
            }
        }
        return res;
    }
}
